<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新建订单界面</title>
    {% load static %}
    <!-- 引入 layui.css -->
    <link href="{%static '/css/layui.css'%}" rel="stylesheet">
</head>
<body>
<div class="layui-container">
    <div class="layui-form">
        <input type="hidden" id="order_id" />
        <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">订单编号</label>
                <div class="layui-input-inline layui-input-wrap">
                  <input type="text" placeholder="订单编号" class="layui-input layui-disabled" disabled />
                </div>

              <label class="layui-form-label">订单日期</label>
                <div class="layui-input-inline layui-input-wrap">
                  <input type="text" placeholder="订单日期" class="layui-input layui-disabled" disabled />
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">处方</label>
                <div class="layui-input-inline layui-input-wrap">
                  <select id="prescription" name="prescription" lay-verify="required" lay-search>
                        <option value="">请选择处方</option>
                        {% for prescription_obj in prescription_list%}
                        <option value="{{prescription_obj.id}}">{{prescription_obj.OrderNum}}</option>
                        {% endfor %}
                  </select>
                </div>

                <label class="layui-form-label">取药地点</label>
                <div class="layui-input-inline layui-input-wrap">
                  <select id="rad" name="rad" lay-verify="required" lay-search>
                        <option value="">请选择取药地点</option>
                        {% for rad_obj in rad_list%}
                        <option value="{{rad_obj.id}}">{{rad_obj.name}}{{rad_obj.place}}</option>
                        {% endfor %}
                  </select>
                </div>

                <label class="layui-form-label">提货方式</label>
                <div class="layui-input-inline layui-input-wrap">
                  <select id="pick_type" name="pick_type" lay-verify="required" lay-search>
                        <option value="">请选择提货方式</option>
                        <option value="自提">自提</option>
                        <option value="顺丰">顺丰</option>
                  </select>
                </div>
            </div>
        </div>
    </div>
    <button type="button" id="btn_save" class="layui-btn">保存</button>
    <table class="layui-table" style="height:full-150" id="infoTable"></table>
</div>

<script id="colBar" type="text/html">
    <button type="button" class="layui-btn layui-btn-sm" id="assignbtn" lay-event="dispen_event">
        摆药
    </button>
</script>
<!-- 引入 layui.js -->
<script src="{%static '/js/layui.min.js'%}" charset="utf-8"></script>
<script>
    layui.use(['layer','jquery','table','laydate','form'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            table = layui.table,
            laydate = layui.laydate,
            form = layui.form;

<!--        保存按钮-->
        $("#btn_save").click(function(){
           var prescription_options = $("#prescription").val()
           var rad_options = $("#rad").val()
           var pick_type_options = $("#pick_type").val()
           if(prescription_options=="" || rad_options=="")
           {
               layer.msg("请选择处方和Rad");
               return;
           }
           order_id = $("#order_id").val();
           if(order_id>0){
               layer.msg("执行保存操作")
           }
           else{
               $.ajax({
                   url:"/api/order/Order/",
                   type:"POST",
                   dataType:"json",
                   data:{
                       "prescription":prescription_options,
                       "rad":rad_options,
                       "order_date":'2024-03-21',
                       "pick_type":pick_type_options,
                       "staruser":{{user_id}}
                   },
                   success:function(data){
                       if(data.code="200"){
                           $("#drug_id").val("");
                           $("#tablet_info").val("");
                           $("#MedNote").val("");
                           $("#MedSpec").val("");
                           $("#drtscd").val("");
                           form.render('select')
                           table.reload('infoTable',function(){
                               page:{curr:1}
                           });
                       }
                   }
               });
           }
        });
        table.render({
           elem:"#infoTable",
           url:"/api/order/Order/?user_id={{user_id}}",
           height:'full-215',
           page:true,
           cols:[[
               {toolbar:'#colBar',title:"操作",width:140,fixed:'right'},
               {title:'序号',width:70,type:"numbers"},
               {field:'ordernum',title:"订单号" },
               {field:'order_date',title:"订单日期" },
               {field:'Prescription_Num',title:"处方号" },
               {field:'rad_name',title:"Rad" },
               {field:'OrderStatus_name',title:"状态" },
               {field:'pick_type',title:"提货方式" },
           ]],
           //修改参数
           request:{
               "pageName":"pageNum",
               "limitName":"pageSize"
           },
           response:{
               statusName:'code',     //数据状态的字段名称，默认：codes
               countName: 'count',    //数据总数的字段名称，默认：count
               dataName:'data',       //数据列表的字段名称，默认：data
           },
           parseData:function(res){
               d = {
                   "code":0,
                   "count":res.data.count,
                   "data":res.data.results
               }
               return d
           }
        });
        table.on('tool(infoTable)',function(obj){
           if(obj.event == 'dispen_event'){
               order_obj = obj.data;
               if(order_obj.OrderStatus=="1"){
                   layer.msg("不允许重复摆药");
               }
               else{
                   $.ajax({
                       url:"/api/prescription/create_rad_preceipt/",
                       type:"put",
                       dataType:"json",
                       data:{
                           "id":order_obj.id,
                       },
                       success:function(data){
                           if(data.code=="200"){
                               layer.msg(data.msg,{time:1*1000},function(){
                                   table.reload('infoTable',function(){
                                       page:{curr:1}
                                   });
                               });
                           }
                       }
                   });
               }
           }
        });
    });
</script>
</body>
</html>